package com.wowoai.chatai.repository.impl;

import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Sorts;
import com.github.wenhao.jpa.Specifications;
import com.wowoai.chatai.entity.UserDrawingEntity;
import com.wowoai.chatai.repository.UserDrawingRepositoryI;
import com.wowoai.chatai.repository.jpa.UserDrawingJpaRepository;
import com.wowoai.common.page.Pagination;
import com.wowoai.common.reponsitory.DataJpaBaseDao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 持久化层 DataJpaBaseDao中实现了UserRepositoryI 中的基本接口
 */
@Repository
public class UserDrawingRepositoryImpl extends DataJpaBaseDao<UserDrawingEntity,Long> implements UserDrawingRepositoryI {

    private final UserDrawingJpaRepository userDrawingJpaRepository;

    public UserDrawingRepositoryImpl(UserDrawingJpaRepository dataJpaRepository) {
        super(dataJpaRepository);
        userDrawingJpaRepository = dataJpaRepository;
    }

    @Override
    public Pagination<UserDrawingEntity> getDrawingImagePage(Integer pageNum, Integer pageSize, String phoneNumber) {
        PredicateBuilder<UserDrawingEntity> builder = Specifications.and();
        builder.eq("deleted", Boolean.FALSE);
        builder.eq("phoneNumber", phoneNumber);
        Sort sort = Sorts.builder().desc("createTime").build();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize,sort);
        Page<UserDrawingEntity> userDrawingEntitiesPage= userDrawingJpaRepository.findAll(builder.build(), pageable);
        List<UserDrawingEntity> userDrawingEntityList = userDrawingEntitiesPage.getContent();
        return new Pagination<>(userDrawingEntitiesPage.getPageable().getPageNumber() + 1, userDrawingEntitiesPage.getPageable().getPageSize(),
                (int) userDrawingEntitiesPage.getTotalElements(), userDrawingEntityList);

    }

    @Override
    public List<UserDrawingEntity> getUserImageList(String phoneNumber, String taskId) {
        PredicateBuilder<UserDrawingEntity> builder = Specifications.and();
        builder.eq("phoneNumber", phoneNumber);
        builder.eq("taskId",taskId);
        builder.eq("deleted", Boolean.FALSE);
        return userDrawingJpaRepository.findAll(builder.build());
    }
}
